package pakovan;

import java.io.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 03.02.12
 * Time: 9:56
 * To change this template use File | Settings | File Templates.
 */
class IOFile {
    public void writeToFile(String fileName, ArrayList<Wing> al) {
        String[] fileExpansion = fileName.split("\\.");
        if (fileExpansion[1].equals("csv")) {
            writeCSV(fileName, al);
        } else if (fileExpansion[1].equals("xml")) {
            writeXML(fileName, al);
        } else if (fileExpansion[1].equals("txt")) {
            writeTXT(fileName, al);
        }
    }

    private void writeCSV(String fileName, ArrayList<Wing> al) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            for (int i = 0; i < al.size(); i++) {
                Wing w = (Wing) al.get(i);
                if (w.getClass().toString().equals("class packovan.Fighter")) {
                    Fighter ft = (Fighter) w;
                    writer.write("Fighter;" + ft.MaxSpeed() + ";" + ft.MachineType() + ";" + ft.EngineType() + ";"
                            + ft.MainWeapon() + "\n");
                } else if (w.getClass().toString().equals("packovan.AirBus")) {
                    AirBus ab = (AirBus) w;
                    writer.write("AirBus;" + ab.MaxSpeed() + ";" + ab.MachineType() + ";" + ab.Passengers() + ";"
                            + ab.Company() + "\n");
                }
            }
        } catch (IOException ex) {
            System.out.println(ex);
        } finally {
            writer.close();
        }
    }

    private void writeXML(String fileName, ArrayList<Wing> al) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            writer.write("<?xml version=\"1.0\"?>\n");
            writer.write("<Wing>\n");
            for (int i = 0; i < al.size(); i++) {
                Wing w = (Wing) al.get(i);
                if (w.getClass().toString().equals("class packovan.Fighter")) {
                    Fighter ft = (Fighter) w;
                    writer.write("<Fighter>\n" + "<MaxSpeed>\n" + ft.MaxSpeed() + "\n" + "</MaxSpeed>\n" +
                            "<MachineType>\n" + ft.MachineType() + "\n" + "</MachineType>\n" + "<EngineType>\n" +
                            ft.EngineType() + "\n" + "</EngineType>\n" + "<MainWeapon>\n" + ft.MainWeapon() + "\n" +
                            "</MainWeapon>\n" + "</Fighter>\n");
                } else if (w.getClass().toString().equals("class packovan.AirBus")) {
                    AirBus ab = (AirBus) w;
                    writer.write("<AirBus>\n" + "<MaxSpeed>\n" + ab.MaxSpeed() + "\n" + "</MaxSpeed>\n" +
                            "<MachineType>\n" + ab.MachineType() + "\n" + "</MachineType>\n" + "<Passengers>\n" +
                            ab.Passengers() + "\n" + "</Passengers>\n" + "<Company>\n" + ab.Company() + "\n" +
                            "</Company>\n" + "</AirBus>\n");
                }
            }
            writer.write("</Wing>");
        } catch (IOException ex) {
            System.out.println(ex);
        } finally {
            writer.close();
        }
    }

    private void writeTXT(String fileName, ArrayList<Wing> al) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            for (int i = 0; i < al.size(); i++) {
                Wing w = (Wing) al.get(i);
                if (w.getClass().toString().equals("class packovan.Fighter")) {
                    Fighter ft = (Fighter) w;
                    writer.write("Fighter " + ft.MaxSpeed() + " " + ft.MachineType() + " " + ft.EngineType() + " "
                            + ft.MainWeapon() + "\n");
                } else if (w.getClass().toString().equals("class packovan.AirBus")) {
                    AirBus ab = (AirBus) w;
                    writer.write("AirBus " + ab.MaxSpeed() + " " + ab.MachineType() + " " + ab.Passengers() + " "
                            + ab.Company() + "\n");
                }
            }
        } catch (IOException ex) {
            System.out.println(ex);
        } finally {
            writer.close();
        }
    }

    public void readFromFile(String fileName, ArrayList<Wing> al) {
        al.clear(); //remove all object from array list
        String[] fileExpansion = fileName.split("\\.");
        ArrayList<String> table = new ArrayList<String>();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (in.ready()) {
                table.add(in.readLine());
            }
            in.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
        //
        try {
            if (fileExpansion[1].equals("csv")) {
                recoveryFromCSV(table, al);//parsing CSV
            } else if (fileExpansion[1].equals("xml")) {
                recoveryFromXML(table, al);//parsing XML
            } else if (fileExpansion[1].equals("txt")) {
                recoveryFromTXT(table, al);//parsing TXT
            }
        } catch (MyException ex) {
            System.out.println(ex);
        }
    }

    public void recoveryFromCSV(ArrayList<String> file, ArrayList<Wing> al) throws MyException {
        if (file.isEmpty()) throw new MyException("File is empty.");
        for (int i = 0; i < file.size(); i++) {
            String[] recoverWings = file.get(i).split("\\;");
            if (recoverWings.length != 5) {
                throw new MyException("Wrong elements.");
            }
            for (String p : recoverWings) {
                if (p.equals("")) throw new MyException("One of value is null.");
            }
            addToCollection(recoverWings, al);
        }
    }

    public void recoveryFromXML(ArrayList<String> file, ArrayList<Wing> al) throws MyException {
        if (file.isEmpty()) throw new MyException("File is empty.");
        Stack<String> tag = new Stack<String>();
        String[] xml = new String[5];
        String end = "Wing";
        int k = 0;
        if (file.size() < 15) throw new MyException("File is not correct.");
        for (int i = 1; i < file.size(); i++) {
            StringBuffer text = new StringBuffer(file.get(i));
            if ((text.charAt(0) == '<') && (text.charAt(1) != '/')) {
                String line = text.substring(1, text.length() - 1).toString();
                tag.add(line);
                if (line.equals("Fighter") || line.equals("AirBus")) {
                    xml[k] = line;
                    k++;
                }
            } else if ((text.charAt(0) == '<') && (text.charAt(1) == '/')) {
                StringBuffer text2 = new StringBuffer(file.get(i));
                String p1 = text2.substring(2, text2.length() - 1).toString();
                String p2 = tag.pop();
                if (p1.equals(end)) System.out.println("File read successfull.");
                else if (p1.equals(p2)) {
                    k = 0;
                    addToCollection(xml, al);
                }
                if (p1.equals(p2) != true) throw new MyException("File is not correct.");
            } else if (text.charAt(0) != '<') {
                xml[k++] = text.toString();
                StringBuffer text2 = new StringBuffer(file.get(i + 1));
                if ((text2.charAt(0) == '<') && (text2.charAt(1) == '/')) {
                    String p1 = text2.substring(2, text2.length() - 1).toString();
                    String p2 = tag.pop();
                    if (p1.equals(p2)) {
                        i = i + 1;
                    } else throw new MyException("File is not correct.");
                } else throw new MyException("File is not correct.");
            }
        }
    }

    public void recoveryFromTXT(ArrayList<String> file, ArrayList<Wing> al) throws MyException {
        if (file.isEmpty()) throw new MyException("File is empty.");
        for (int i = 0; i < file.size(); i++) {
            String[] recoverWings = file.get(i).split("\\ ");
            if (recoverWings.length != 5) {
                throw new MyException("Wrong elements.");
            }
            for (String p : recoverWings) {
                if (p.equals("")) throw new MyException("One of values is null.");
            }
            addToCollection(recoverWings, al);
        }
    }

    private void addToCollection(String[] recoverWings, ArrayList<Wing> al) {
        if (recoverWings[0].equals("Fighter")) {
            Fighter ft = new Fighter(Integer.parseInt(recoverWings[1]), recoverWings[2], recoverWings[3], recoverWings[4]);
            al.add(ft);
        } else if (recoverWings[0].equals("AirBus")) {
            AirBus ab = new AirBus(Integer.parseInt(recoverWings[1]), recoverWings[2], Integer.parseInt(recoverWings[3]), recoverWings[4]);
            al.add(ab);
        }
    }


}
